<!DOCTYPE HTML>
<html lang="zh-CN">

<head>
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="涛哥的博客">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <meta name="theme-version" content="1.2.3">
    <meta name="root" content="/">
    <link rel="dns-prefetch" href="https://jaredtao.github.io">
    <!--SEO-->

<meta name="keywords" content="Qt,Qml,QtQuick,组件化编程" />


<meta name="description" content="
简介
效果预览
必要的基础
QObject自定义属性
全局单例


实现
皮肤的配置和原理
皮肤选择器
带三角形尖尖的弹窗组件



简介本文是《玩转Qml》系列文章的第三篇，涛哥将教大家，如..." />


<meta name="robots" content="all" />
<meta name="google" content="all" />
<meta name="googlebot" content="all" />
<meta name="verify" content="all" />
    <!--Title-->

<title>
    
    玩转Qml(3)-换皮肤 |
    
    涛哥的博客
</title>

<link rel="alternate" href="/atom.xml" title="涛哥的博客" type="application/atom+xml">


<link rel="icon" href="/favicon.ico">

    


<link rel="stylesheet" href="/css/bootstrap.min.css?rev=3.3.7.css">
<link rel="stylesheet" href="/css/font-awesome.min.css?rev=4.7.0.css">
<link rel="stylesheet" href="/css/style.css?rev=@@hash.css">

    
<div class="hide">
    <script type="text/javascript">
    var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
    document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
    </script>
</div>




    

<script>
(function() {
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    } else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>

<meta name="generator" content="Hexo 4.2.1"></head>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->
<body>
    <header class="main-header"  style="background-image:url(
    http://snippet.shenliyang.com/img/banner.jpg)"
     >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title='JaredTao'>
            <img src="/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
            <!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
            <h2>
                单枪匹马亦对饮，历经磨难记初心
            </h2>
            
        </div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                        <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="https://jaredtao.github.io">
                        涛哥的博客</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                        <li role="presentation" class="text-center">
                            <a href="/"><i class="fa "></i>
                                首页</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/categories/玩转Qt/"><i class="fa "></i>
                                玩转Qt</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/categories/玩转Qml/"><i class="fa "></i>
                                玩转Qml</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/categories/玩转QtQuick/"><i class="fa "></i>
                                玩转QtQuick</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/categories/涛哥的博客/"><i class="fa "></i>
                                涛哥的博客</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/archives/"><i class="fa "></i>
                                时间轴</a>
                        </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="玩转Qml(3)-换皮肤">
            
            玩转Qml(3)-换皮肤
            
        </h1>
        <div class="post-meta">
    
    <span class="categories-meta fa-wrap">
        <i class="fa fa-folder-open-o"></i>
        <a class="category-link" href="/categories/%E7%8E%A9%E8%BD%ACQml/">玩转Qml</a>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
            <a class="tag-link" href="/tags/Qml/" rel="tag">Qml</a> <a class="tag-link" href="/tags/Qt/" rel="tag">Qt</a> <a class="tag-link" href="/tags/QtQuick/" rel="tag">QtQuick</a> <a class="tag-link" href="/tags/%E7%BB%84%E4%BB%B6%E5%8C%96%E7%BC%96%E7%A8%8B/" rel="tag">组件化编程</a>
            
        </span>
    </span>
    
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta">
            2019/05/12</span>
    </span>
    
    <span class="fa-wrap">
        <i class="fa fa-eye"></i>
        <span id="busuanzi_value_page_pv"></span>
    </span>
    
    
</div>
        
        
        <p class="fa fa-exclamation-triangle warning">
            本文于<strong>
                2038</strong>
            天之前发表，文中内容可能已经过时。
        </p>
        
    </div>
    
<div id="post-gallery">
    
    <img src="/images/Qml3/1.webp" alt="gallery-img" class="gallery">
    
</div>

    <div class="post-body post-content">
        <ul>
<li><a href="#%E7%AE%80%E4%BB%8B">简介</a></li>
<li><a href="#%E6%95%88%E6%9E%9C%E9%A2%84%E8%A7%88">效果预览</a></li>
<li><a href="#%E5%BF%85%E8%A6%81%E7%9A%84%E5%9F%BA%E7%A1%80">必要的基础</a><ul>
<li><a href="#qobject%E8%87%AA%E5%AE%9A%E4%B9%89%E5%B1%9E%E6%80%A7">QObject自定义属性</a></li>
<li><a href="#%E5%85%A8%E5%B1%80%E5%8D%95%E4%BE%8B">全局单例</a></li>
</ul>
</li>
<li><a href="#%E5%AE%9E%E7%8E%B0">实现</a><ul>
<li><a href="#%E7%9A%AE%E8%82%A4%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E5%8E%9F%E7%90%86">皮肤的配置和原理</a></li>
<li><a href="#%E7%9A%AE%E8%82%A4%E9%80%89%E6%8B%A9%E5%99%A8">皮肤选择器</a></li>
<li><a href="#%E5%B8%A6%E4%B8%89%E8%A7%92%E5%BD%A2%E5%B0%96%E5%B0%96%E7%9A%84%E5%BC%B9%E7%AA%97%E7%BB%84%E4%BB%B6">带三角形尖尖的弹窗组件</a></li>
</ul>
</li>
</ul>
<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>本文是《玩转Qml》系列文章的第三篇，涛哥将教大家，如何在Qml中实现动态换皮肤。顺带会分享一些Qt小技巧。</p>
<h2 id="源码"><a href="#源码" class="headerlink" title="源码"></a>源码</h2><p>《玩转Qml》系列文章，配套了一个优秀的开源项目:TaoQuick</p>
<p>github <a href="https://github.com/jaredtao/TaoQuick" target="_blank" rel="noopener">https://github.com/jaredtao/TaoQuick</a></p>
<p>访问不了或者速度太慢，可以用国内的镜像网站gitee</p>
<p><a href="https://gitee.com/jaredtao/TaoQuick" target="_blank" rel="noopener">https://gitee.com/jaredtao/TaoQuick</a></p>
<h2 id="效果预览"><a href="#效果预览" class="headerlink" title="效果预览"></a>效果预览</h2><p>效果类似于网易云音乐</p>
<p><img src="/images/Qml3/1.webp" alt="预览"></p>
<p>顺便说一下，这是涛哥创建的TaoQuick项目，后续的各种组件、效果会全部集中在这个项目里。</p>
<p>文章中涉及的代码，都会先贴出来。整个工程的代码，在积累到一定程度后，会开放在github上。</p>
<h2 id="必要的基础"><a href="#必要的基础" class="headerlink" title="必要的基础"></a>必要的基础</h2><p>可能有读者会疑惑，涛哥前两篇文章还在讲如何封装基础组件，这第三篇直接就来换皮肤？是不是跨度有点大？</p>
<p>其实换皮肤是一个很基础的功能，如果要做最好在项目初期就做起来，后期想要做换皮肤会困难一些（工作量大）。</p>
<p>如果你的项目做了很多组件化的封装，再做换皮肤会轻松一些。</p>
<h3 id="QObject自定义属性"><a href="#QObject自定义属性" class="headerlink" title="QObject自定义属性"></a>QObject自定义属性</h3><p>Qml中有一个类型叫QtObject，涛哥非常喜欢使用这个类型。</p>
<p>以前在写Qt/C++代码中的自定义QObject时，经常需要写一些自定义的Q_PROPERTY,以及实现set、get函数、change信号</p>
<p>如果纯手工写，挺累人的，涛哥曾写过自动生成器，相信很多人也写过类似的工具。</p>
<p>后来涛哥发现，QtCreator有自动生成的功能，只要写上Q_PROPERTY那一行，再用右键菜单生成即可，</p>
<p>高效率人士也可以使用快捷键，光标放在Q_PROPERTY上，按Alt + Enter。</p>
<p><img src="/images/Qml3/2.gif" alt="预览"></p>
<p>有时候需要把set函数的参数改成const T &amp;类型来减少内存拷贝，并把函数实现移动到cpp文件中。(这都是C++的诟病)</p>
<p>然而，在Qml中，有更加方便的QtObject,</p>
<figure class="highlight dts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class">Item </span>&#123;</span><br><span class="line">    <span class="class">QtObject </span>&#123;  <span class="comment">//定义一个QtObject，相当于Qt/c++代码中的QObject</span></span><br><span class="line"><span class="symbol">        id:</span> dataObj</span><br><span class="line">        property string name: <span class="string">"Hello"</span>   <span class="comment">//这就定义了一个string类型的属性name，初始值设为"Hello"</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//定义完了，已经自带了onNameChanged信号。name被重新赋值时会触发信号</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//给这个信号写一个处理函数，就相当于连接上了槽函数。</span></span><br><span class="line"><span class="symbol">        onNameChanged:</span> &#123;</span><br><span class="line">            console.info(<span class="string">"name:"</span>, name)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">    <span class="class">Button </span>&#123;</span><br><span class="line">        ...</span><br><span class="line"><span class="symbol">        onClicked:</span> &#123;    <span class="comment">//演示: 按钮按下时，修改前面QtObject的name属性</span></span><br><span class="line">            dataObj.name = <span class="string">"World"</span>; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>(哈哈，工作量和心理负担一下子减轻了很多，头发的数量也能保住了。再也不想回去写Qt/C++的属性了。)</p>
<h3 id="全局单例"><a href="#全局单例" class="headerlink" title="全局单例"></a>全局单例</h3><p>涛哥写了一个单独的qml文件，顶层就是一个QtObject类型，里面会有一大堆属性。</p>
<p>颜色、字体一类的配置都在这里。</p>
<figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// GlobalConfig.qml</span></span><br><span class="line"><span class="keyword">import</span> QtQuick <span class="number">2.0</span></span><br><span class="line"><span class="title">QtObject</span> &#123;</span><br><span class="line">    <span class="keyword">property</span><span class="string"> color titleBackground</span>: <span class="string">"#c62f2f"</span>   <span class="comment">//标题栏的背景色</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color background</span>: <span class="string">"#f6f6f6"</span>        <span class="comment">//标题栏之外的部分的背景色</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color reserverColor</span>: <span class="string">"#ffffff"</span>     <span class="comment">//与背景色相反的对比色</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color textColor</span>: <span class="string">"black"</span>           <span class="comment">//文本颜色</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后在main.qml中实例化它</p>
<figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// main.qml</span></span><br><span class="line"><span class="title">Item</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">800</span></span><br><span class="line">    <span class="attribute">height</span>: <span class="number">600</span></span><br><span class="line">    <span class="title">GlobalConfig</span> &#123;</span><br><span class="line">        <span class="attribute">id:</span><span class="string"> gConfig</span></span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Qml有个特性，子页面实例可以通过id访问父页面中的实例，读 写其属性、调用其函数。</p>
<p>在main.qml中实例化的对象，相当于是全局的了，它的id是可以在所有main.qml的子页面中访问到的。</p>
<p>（当然还有一种方式，通过qmldir指定单例，这种留着后面再说）</p>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><h3 id="皮肤的配置和原理"><a href="#皮肤的配置和原理" class="headerlink" title="皮肤的配置和原理"></a>皮肤的配置和原理</h3><p>下面是TaoQuick中使用的gConfig</p>
<figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// GlobalConfig.qml</span></span><br><span class="line"><span class="title">QtObject</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color titleBackground</span>: <span class="string">"#c62f2f"</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color background</span>: <span class="string">"#f6f6f6"</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color reserverColor</span>: <span class="string">"#ffffff"</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color textColor</span>: <span class="string">"black"</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color splitColor</span>: <span class="string">"gray"</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">property</span><span class="string"> int currentTheme</span>: <span class="number">0</span></span><br><span class="line">    <span class="attribute">onCurrentThemeChanged</span>: &#123;</span><br><span class="line">        <span class="keyword">var</span> t = themes.get(currentTheme)</span><br><span class="line">        titleBackground = t.titleBackground</span><br><span class="line">        background = t.background</span><br><span class="line">        textColor = t.textColor</span><br><span class="line">    &#125;</span><br><span class="line">    readonly <span class="keyword">property</span><span class="string"> ListModel themes</span>: <span class="title">ListModel</span> &#123;</span><br><span class="line">        <span class="title">ListElement</span> &#123;</span><br><span class="line">            <span class="attribute">name</span>: qsTr(<span class="string">"一品红"</span>)</span><br><span class="line">            <span class="attribute">titleBackground</span>: <span class="string">"#c62f2f"</span></span><br><span class="line">            <span class="attribute">background</span>: <span class="string">"#f6f6f6"</span></span><br><span class="line">            <span class="attribute">textColor</span>: <span class="string">"#5c5c5c"</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title">ListElement</span> &#123;</span><br><span class="line">            <span class="attribute">name</span>: qsTr(<span class="string">"高冷黑"</span>)</span><br><span class="line">            <span class="attribute">titleBackground</span>: <span class="string">"#191b1f"</span></span><br><span class="line">            <span class="attribute">background</span>: <span class="string">"#222225"</span></span><br><span class="line">            <span class="attribute">textColor</span>: <span class="string">"#adafb2"</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title">ListElement</span> &#123;</span><br><span class="line">            <span class="attribute">name</span>: qsTr(<span class="string">"淑女粉"</span>)</span><br><span class="line">            <span class="attribute">titleBackground</span>: <span class="string">"#faa0c5"</span></span><br><span class="line">            <span class="attribute">background</span>: <span class="string">"#f6f6f6"</span></span><br><span class="line">            <span class="attribute">textColor</span>: <span class="string">"#5c5c5c"</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title">ListElement</span> &#123;</span><br><span class="line">            <span class="attribute">name</span>: qsTr(<span class="string">"富贵金"</span>)</span><br><span class="line">            <span class="attribute">titleBackground</span>: <span class="string">"#fed98f"</span></span><br><span class="line">            <span class="attribute">background</span>: <span class="string">"#f6f6f6"</span></span><br><span class="line">            <span class="attribute">textColor</span>: <span class="string">"#5c5c5c"</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title">ListElement</span> &#123;</span><br><span class="line">            <span class="attribute">name</span>: qsTr(<span class="string">" 清爽绿"</span>)</span><br><span class="line">            <span class="attribute">titleBackground</span>: <span class="string">"#58c979"</span></span><br><span class="line">            <span class="attribute">background</span>: <span class="string">"#f6f6f6"</span></span><br><span class="line">            <span class="attribute">textColor</span>: <span class="string">"#5c5c5c"</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title">ListElement</span> &#123;</span><br><span class="line">            <span class="attribute">name</span>: qsTr(<span class="string">"苍穹蓝"</span>)</span><br><span class="line">            <span class="attribute">titleBackground</span>: <span class="string">"#67c1fd"</span></span><br><span class="line">            <span class="attribute">background</span>: <span class="string">"#f6f6f6"</span></span><br><span class="line">            <span class="attribute">textColor</span>: <span class="string">"#5c5c5c"</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>涛哥在所有的Page页面中，相关颜色设置都绑定到gConfig的相应属性上。</p>
<p>那么换皮肤，只需要修改gConfig中的颜色相关属性即可。因为修改属性时会触发change信号，而所有的Page都绑定了</p>
<p>gConfig的属性，会自动在发生change时重新读属性，修改后的颜色自动就生效了。</p>
<p>这里顺带说一下，主题的颜色相关属性越少越好，因为太多了不容易识别、不好维护，</p>
<p>之前的文章《玩转Qml(1)-从按钮开始》中提到的Qt.lighter和Qt.darker,</p>
<p>就是一种减少颜色属性数量的神器。</p>
<h3 id="皮肤选择器"><a href="#皮肤选择器" class="headerlink" title="皮肤选择器"></a>皮肤选择器</h3><p>再来看一下，涛哥参考 网易云音乐 做的皮肤选择器</p>
<figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="title">TImageBtn</span> &#123;     <span class="comment">//图片按钮，参考文章1</span></span><br><span class="line">    <span class="attribute">width</span>: <span class="number">20</span></span><br><span class="line">    <span class="attribute">height</span>: <span class="number">20</span></span><br><span class="line">    <span class="attribute">anchors.verticalCenter</span>: <span class="built_in">parent</span>.verticalCenter</span><br><span class="line">    <span class="attribute">imageUrl</span>: containsMouse ? <span class="string">"qrc:/Image/Window/skin_white.png"</span> : <span class="string">"qrc:/Image/Window/skin_gray.png"</span></span><br><span class="line">    <span class="attribute">onClicked</span>: &#123;</span><br><span class="line">        skinBox.show()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title">TPopup</span> &#123;    <span class="comment">//自定义的弹窗，带三角尖尖的那个。</span></span><br><span class="line">        <span class="attribute">id:</span><span class="string"> skinBox</span></span><br><span class="line">        <span class="attribute">barColor</span>: gConfig.reserverColor</span><br><span class="line">        <span class="attribute">backgroundWidth</span>: <span class="number">280</span></span><br><span class="line">        <span class="attribute">backgroundHeight</span>: <span class="number">180</span></span><br><span class="line">        <span class="attribute">contentItem</span>: <span class="title">GridView</span> &#123;</span><br><span class="line">            <span class="attribute">anchors.fill</span>: <span class="built_in">parent</span></span><br><span class="line">            <span class="attribute">anchors.margins</span>: <span class="number">10</span></span><br><span class="line">            <span class="attribute">model</span>: gConfig.themes</span><br><span class="line">            <span class="attribute">cellWidth</span>: <span class="number">80</span></span><br><span class="line">            <span class="attribute">cellHeight</span>: <span class="number">80</span></span><br><span class="line">            <span class="attribute">delegate</span>: <span class="title">Item</span> &#123;</span><br><span class="line">                <span class="attribute">width</span>: <span class="number">80</span></span><br><span class="line">                <span class="attribute">height</span>: <span class="number">80</span></span><br><span class="line">                <span class="title">Rectangle</span> &#123; <span class="comment">//表示主题色的色块</span></span><br><span class="line">                    <span class="attribute">anchors.fill</span>: <span class="built_in">parent</span></span><br><span class="line">                    <span class="attribute">anchors.margins</span>: <span class="number">4</span></span><br><span class="line">                    <span class="attribute">height</span>: width</span><br><span class="line">                    <span class="attribute">color</span>: model.titleBackground</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="title">Rectangle</span> &#123; <span class="comment">//主题色边框，鼠标悬浮时显示</span></span><br><span class="line">                    <span class="attribute">anchors.fill</span>: <span class="built_in">parent</span></span><br><span class="line">                    <span class="attribute">color</span>: <span class="string">"transparent"</span></span><br><span class="line">                    <span class="attribute">border.color</span>: model.titleBackground</span><br><span class="line">                    <span class="attribute">border.width</span>: <span class="number">2</span></span><br><span class="line">                    <span class="attribute">visible</span>: a.containsMouse</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="title">Text</span> &#123;  <span class="comment">//主题名字</span></span><br><span class="line">                    <span class="title">anchors</span> &#123;</span><br><span class="line">                        <span class="attribute">left</span>: <span class="built_in">parent</span>.left</span><br><span class="line">                        <span class="attribute">bottom</span>: <span class="built_in">parent</span>.bottom</span><br><span class="line">                        <span class="attribute">leftMargin</span>: <span class="number">8</span></span><br><span class="line">                        <span class="attribute">bottomMargin</span>: <span class="number">8</span></span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="attribute">color</span>: <span class="string">"white"</span></span><br><span class="line">                    <span class="attribute">text</span>: model.name</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="title">Rectangle</span> &#123; <span class="comment">//右下角圆圈圈，当前选中的主题</span></span><br><span class="line">                    <span class="attribute">x</span>: <span class="built_in">parent</span>.width - width</span><br><span class="line">                    <span class="attribute">y</span>: <span class="built_in">parent</span>.height - height</span><br><span class="line">                    <span class="attribute">width</span>: <span class="number">20</span></span><br><span class="line">                    <span class="attribute">height</span>: width</span><br><span class="line">                    <span class="attribute">radius</span>: width / <span class="number">2</span></span><br><span class="line">                    <span class="attribute">color</span>: model.titleBackground</span><br><span class="line">                    <span class="attribute">border.width</span>: <span class="number">3</span></span><br><span class="line">                    <span class="attribute">border.color</span>: gConfig.reserverColor</span><br><span class="line">                    <span class="attribute">visible</span>: gConfig.currentTheme === index</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="title">MouseArea</span> &#123; <span class="comment">//鼠标状态</span></span><br><span class="line">                    <span class="attribute">id:</span><span class="string"> a</span></span><br><span class="line">                    <span class="attribute">anchors.fill</span>: <span class="built_in">parent</span></span><br><span class="line">                    <span class="attribute">hoverEnabled</span>: <span class="literal">true</span></span><br><span class="line">                    <span class="attribute">onClicked</span>: &#123;    <span class="comment">//切主题操作</span></span><br><span class="line">                        gConfig.currentTheme = index</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="带三角形尖尖的弹窗组件"><a href="#带三角形尖尖的弹窗组件" class="headerlink" title="带三角形尖尖的弹窗组件"></a>带三角形尖尖的弹窗组件</h3><figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// TPopup.qml</span></span><br><span class="line"><span class="keyword">import</span> QtQuick <span class="number">2.9</span></span><br><span class="line"><span class="keyword">import</span> QtQuick.Controls <span class="number">2.5</span></span><br><span class="line"><span class="title">Item</span> &#123;</span><br><span class="line">    <span class="attribute">id:</span><span class="string"> root</span></span><br><span class="line">    <span class="attribute">anchors.fill</span>: <span class="built_in">parent</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> alias popupVisible</span>: popup.visible</span><br><span class="line">    <span class="keyword">property</span><span class="string"> alias contentItem</span>: popup.contentItem</span><br><span class="line">    <span class="keyword">property</span><span class="string"> color barColor</span>: <span class="string">"white"</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> alias backgroundItem</span>: background</span><br><span class="line">    <span class="keyword">property</span><span class="string"> real backgroundWidth</span>: <span class="number">200</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> real backgroundHeight</span>: <span class="number">160</span></span><br><span class="line">    <span class="keyword">property</span><span class="string"> color borderColor</span>:  barColor</span><br><span class="line">    <span class="keyword">property</span><span class="string"> real borderWidth</span>: <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">property</span><span class="string"> real verticalOffset</span>: <span class="number">20</span></span><br><span class="line">    <span class="comment">//矩形旋转45度，一半被toolTip遮住(重合)，另一半三角形和ToolTip组成一个带箭头的ToolTip</span></span><br><span class="line">    <span class="title">Rectangle</span> &#123;</span><br><span class="line">        <span class="attribute">id:</span><span class="string"> bar</span></span><br><span class="line">        <span class="attribute">visible</span>: popup.visible</span><br><span class="line">        <span class="attribute">rotation</span>: <span class="number">45</span></span><br><span class="line">        <span class="attribute">width</span>: <span class="number">16</span></span><br><span class="line">        <span class="attribute">height</span>: <span class="number">16</span></span><br><span class="line">        <span class="attribute">color</span>: barColor</span><br><span class="line">        <span class="comment">//水平居中</span></span><br><span class="line">        <span class="attribute">anchors.horizontalCenter</span>: <span class="built_in">parent</span>.horizontalCenter</span><br><span class="line">        <span class="comment">//垂直方向上，由ToolTip的y值，决定位置</span></span><br><span class="line">        <span class="attribute">anchors.verticalCenter</span>: <span class="built_in">parent</span>.bottom</span><br><span class="line">        <span class="attribute">anchors.verticalCenterOffset</span>: verticalOffset</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title">Popup</span> &#123;</span><br><span class="line">        <span class="attribute">id:</span><span class="string"> popup</span></span><br><span class="line">        <span class="attribute">width</span>: backgroundWidth</span><br><span class="line">        <span class="attribute">height</span>: backgroundHeight</span><br><span class="line">        <span class="attribute">background</span>: <span class="title">Rectangle</span> &#123;</span><br><span class="line">            <span class="attribute">id:</span><span class="string"> background</span></span><br><span class="line">            <span class="attribute">color</span>: barColor</span><br><span class="line">            <span class="attribute">radius</span>: <span class="number">8</span></span><br><span class="line">            <span class="attribute">border.color</span>:<span class="attribute">borderColor</span></span><br><span class="line"><span class="attribute">            border.width</span>: borderWidth</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">show</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        popup.x = (root.width - popup.width) / <span class="number">2</span></span><br><span class="line">        popup.y = root.height + verticalOffset</span><br><span class="line">        popupVisible = <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">hide</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        popupVisible = <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>
    
    <div class="reward" ontouchstart>
    <div class="reward-wrap">赏
        <div class="reward-box">
            
            <span class="reward-type">
                <img class="alipay" src="/img/zhifubao.jpg"><b>支付宝打赏</b>
            </span>
            
            
            <span class="reward-type">
                <img class="wechat" src="/img/weixin.jpg"><b>微信打赏</b>
            </span>
            
        </div>
    </div>
    <p class="reward-tip">
        很有收获,请作者喝杯茶
    </p>
</div>
    
    <div class="post-footer">
        <div>
            
            转载声明：
            文章采用<a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>进行许可, 转载请注明出处 © <a href="https://jaredtao.github.io" target="_blank">武威的涛哥</a>
            
            
        </div>
        <div>
            
        </div>
    </div>
</article>
<div class="article-nav prev-next-wrap clearfix">
    
    <a href="/2019/05/12/Qml/%E7%8E%A9%E8%BD%ACQml(4)-I18n%E5%8A%A8%E6%80%81%E5%9B%BD%E9%99%85%E5%8C%96/" class="pre-post btn btn-default" title='玩转Qml(4)-I18n动态国际化'>
        <i class="fa fa-angle-left fa-fw"></i><span class="hidden-lg">上一篇</span>
        <span class="hidden-xs">
            玩转Qml(4)-I18n动态国际化</span>
    </a>
    
    
    <a href="/2019/05/11/Qml/%E7%8E%A9%E8%BD%ACQml(2)-%E5%8F%AF%E4%BB%A5%E6%8B%96%E5%8A%A8%E7%9A%84%E7%BB%84%E4%BB%B6/" class="next-post btn btn-default" title='玩转Qml(2)-可以拖动的组件'>
        <span class="hidden-lg">下一篇</span>
        <span class="hidden-xs">
            玩转Qml(2)-可以拖动的组件</span><i class="fa fa-angle-right fa-fw"></i>
    </a>
    
</div>

<div id="comments">
    

<div id="vcomments" class="valine"></div>

<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="/assets/valine.min.js"></script>

<script>
new Valine({
    av: AV,
    el: '#vcomments',
    appId: 'qegD4552u6FU9HoNDvRsonko-gzGzoHsz',
    appKey: 'eitB2FV5n2qyMHcDM7x6EQpt',
    placeholder: '说点什么吧',
    notify: false,
    verify: true,
    avatar: 'mm',
    meta: 'nick,mail'.split(','),
    pageSize: '10',
    path: window.location.pathname,
    lang: 'zh-CN'.toLowerCase()
})
</script>


</div>


                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">
            文章目录
        </h3>
        
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#简介"><span class="toc-text">简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#源码"><span class="toc-text">源码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#效果预览"><span class="toc-text">效果预览</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#必要的基础"><span class="toc-text">必要的基础</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#QObject自定义属性"><span class="toc-text">QObject自定义属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#全局单例"><span class="toc-text">全局单例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#实现"><span class="toc-text">实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#皮肤的配置和原理"><span class="toc-text">皮肤的配置和原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#皮肤选择器"><span class="toc-text">皮肤选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#带三角形尖尖的弹窗组件"><span class="toc-text">带三角形尖尖的弹窗组件</span></a></li></ol></li></ol>
        
    </div>
</aside>
                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>
<a id="back-to-top" class="icon-btn hide">
    <i class="fa fa-chevron-up"></i>
</a>
    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
    访问量:
    <strong id="busuanzi_value_site_pv">
        <i class="fa fa-spinner fa-spin"></i>
    </strong>
    &nbsp; | &nbsp;
    访客数:
    <strong id="busuanzi_value_site_uv">
        <i class="fa fa-spinner fa-spin"></i>
    </strong>
    
</div>
            </div>
            <div class="col-sm-12">
                <span>Copyright &copy;
                    2019
                    
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>



<script src="/assets/tagcanvas.min.js?rev=2.9.js"></script>

<script>
var tagOption = {
    textColour: '#444', // 字体颜色
    outlineMethod: 'block', // 选中模式
    outlineColour: '#FFDAB9', // 选中模式的颜色
    interval: 30 || 30, // 动画帧之间的时间间隔，值越大，转动幅度越大
    textHeight: 13,
    outlineRadius: 3,
    freezeActive: true || '', // 选中的标签是否继续滚动
    frontSelect: true || '', // 不选标签云后部的标签
    initial: [0.1, -0.1],
    depth: 0.5,
    decel: 0.95,
    maxSpeed: 0.03,
    reverse: true || '', // 是否反向触发
    fadeIn: 500, // 进入动画时间
    wheelZoom: false || '' // 是否启用鼠标滚轮
}
TagCanvas.Start('tag-cloud-3d', '', tagOption);
</script>


<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


<script src="/js/app.js?rev=@@hash.js"></script>

</body>
</html>